package ren.install.web.controller;

import cn.hutool.core.io.IoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import ren.install.web.config.ValueCenter;
import ren.install.web.service.LuceneService;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: zhuyh
 * @date: 2018/11/21
 */
@RestController
@Slf4j
public class AdminController {

    @Autowired
    private ValueCenter valueCenter;

    @Autowired
    private LuceneService luceneService;

    @GetMapping("/admin")
    public String admin() throws Exception {
        return "admin";
    }

    @GetMapping("/indices/refresh")
    public String indicesRefresh() throws Exception {
        luceneService.refresh();
        return "ok";
    }


    @GetMapping("/pull")
    public String pull() throws Exception {
        Process p = null;
        List<String> address = new ArrayList<String>(); //物理网卡列表
        try {

            synchronized (this) {
                ProcessBuilder pb = new ProcessBuilder("git", "pull");
                Map map = pb.environment();
                System.out.println(map);

                pb.directory(new File(valueCenter.getRootFilePath()));
                p = pb.start();
            }

        } catch (Exception e) {
            System.out.println("processBuilder error");
            return e.getMessage();
        }
        return IoUtil.read(p.getInputStream(), "UTF-8");

    }
}
